﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;

using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMCategoryWPMtn : BaseMtn 
    {
        public frmRMCategoryWPMtn()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MtnTabControl = this.tbcMain;
            this.DesignTabsName = "tbpGeneral";
            tbcMain.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.MinimumSize = this.Size;
        }


        #region Define Variable
        private bool mblnSettingStatus = false;
        private bool[] mDtlColumnReadOnlyArray;
        private bool[] mDtlColumnReadOnlyArray_Alter;
        private bool m_bCanTrigerCellValueChangedEven = false ;
        private bool m_bCanTrigerCellValueChangedEven_Alter = false;
        private bool m_bSettingStatus = false;
        private bool m_bDtlChanged = false;
        private bool m_bDtlChanged_Alter = false;

        private System.Windows.Forms.DataGridView.HitTestInfo mhtiDtl = null;
        private enum DtlCol 
        {
            RowSeqNo=0,
            SeqNo,
            WKProductID,
            WKName,
            WKRemark,
            IsKeyWK,
            IsInputWK,
            CategoryName,
            CategoryID,
            RecID,
            ValueChanged,
            DelButton
        }

        private enum DtlColAlter
        {
            RowSeqNo = 0,
            WKProductID,
            WKName,
            WKRemark,
            IsKeyWK,
            IsInputWK,
            CategoryName,
            CategoryID,
            HdrSeqNo,
            RecID,
            ValueChanged,
            DelButton
        }
        #endregion

        #region ***Form Events
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) //激活回车键
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (!(msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN))
            {
                return false;
            }




            bool blnRet = false;
            if (BaseFunctions.TrigerKeyEventByControl(this.ActiveControl, this.dgvDetail.Name))
            {
                blnRet = dgvDetail_KeyDownEvent(keyData);
            }
            else if (BaseFunctions.TrigerKeyEventByControl(this.ActiveControl, this.dgvAlter.Name))
            {
                blnRet = dgvAlter_KeyDownEvent(keyData);
            }
            if (!blnRet)
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
            return blnRet;
        }

        private void frmRMCategoryWPMtn_Load(object sender, EventArgs e)
        {
            this.TableName = "rmCategory";
            //dgList.ColumnWidth = "0,70,180,100,40,20,120,90,130";
            dgList.ConnectionString = DBAccess.ConnectionDBString;
            dgList.TableName = this.TableName;
            dgList.ColumnWidth = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgList.Name);
            string strSql = WP.Commons.BaseFunctions.GetLookUpSQL(this.TableName, this.CurrentMenuInfo.iParameter);
            dgList.Sql = strSql;
            this.formStatus = FormStatus.Query;

            FillLookUpInfo();
        }
        private void frmRMCategoryWPMtn_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataFunctions.SetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.dgList);
            DataFunctions.SetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.dgvDetail);
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!this.UpdateData()) return;
            base.SetStatusAfterUpdate();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (this.ConfirmChange())
            {
                if (!this.UpdateData()) return;
            }
            this.formStatus = FormStatus.AddNew;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!this.DeleteData()) return;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.formStatus = FormStatus.Delete;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.formStatus == FormStatus.AddNew || this.formStatus == FormStatus.Update)
            {
                if (this.ConfirmChange())
                {
                    if (MessageBox.Show(SysVar.SaveChangeTitle, SysVar.WarnTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        if (!this.UpdateData()) return;
                    }
                }
            }
            this.ExitingStatus = true;
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void btnToExcel_Click(object sender, EventArgs e)
        {
            base.ExportToExcel(dgList);
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Next);
        }
        private void btnPrev_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Prev);
        }
        private void btnTop_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Top);
        }
        private void btnBottom_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Bottom);
        }
        public override void SetNavigatorBarTooltip()
        {
            if (this.RecID <= 0)
            {
                this.btnBottom.ToolTipText = "定位到末页";
                this.btnTop.ToolTipText = "定位到首页";
                this.btnNext.ToolTipText = "定位到下一页";
                this.btnPrev.ToolTipText = "定位到前一页";
            }
            else
            {
                this.btnBottom.ToolTipText = "定位到末记录";
                this.btnTop.ToolTipText = "定位到首记录";
                this.btnNext.ToolTipText = "定位到下一条记录";
                this.btnPrev.ToolTipText = "定位到上一条记录";
            }
        }

        private void dgList_DefineColumn(object sender, EventArgs e)
        {
            base.SelectListViewField(dgList);
        }
        private void dgList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && dgList.CurrentRow != null)
            {
                this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }
        private void dgList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgList.CurrentRow != null && BaseFunctions.IsDataGridCell(this.dgList, e))
            {
                this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }

        protected override void Field_Leave(object sender, System.EventArgs e)
        {
            base.Field_Leave(sender, e);
            Control con = sender as Control;
            if (con == null) return;

            //below is some speci field , need do some speci work
            if (con.Name == txtCategoryID.Name)
            {
            }
        }
        protected override void Field_Change(object sender, EventArgs e)
        {
            base.Field_Change(sender, e);
            if (this.formStatus == FormStatus.Delete) return;
            Control con = sender as Control;
            if (con == null) return;

        }
        protected override void Field_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.Field_KeyPress(sender, e);
        }

        private void tbcMain_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if ((this.formStatus == FormStatus.Query || this.formStatus == FormStatus.Delete) &&
                this.dgList.Rows.Count == 0 && e.TabPageIndex > 0)
            {
                e.Cancel = true;
                return;
            }

            if (e.TabPageIndex == 0)
            {
                if (this.formStatus == FormStatus.AddNew || this.formStatus == FormStatus.Update)
                {
                    if (this.ConfirmChange())
                    {
                        if (MessageBox.Show(SysVar.SaveChangeTitle, SysVar.WarnTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            if (!this.UpdateData())
                            {
                                if (m_bTabPageNeedCancel)
                                    e.Cancel = true;

                                m_bTabPageNeedCancel = true;
                                return;
                            }
                        }
                    }
                    this.formStatus = FormStatus.Delete;
                }
                else if (this.formStatus == FormStatus.Query)
                {
                    this.formStatus = FormStatus.Delete;
                }
                this.dgList.RefreshData();
                this.dgList.Focus();
            }
            else if (e.TabPageIndex != 0 && this.formStatus != FormStatus.AddNew)
            {
                if (this.RecID == -1)
                    this.RecID = Convert.ToInt64(dgList.RecID);

               
                if (this.dgvAlter.CurrentRow != null)
                    FillDtl_Alter(txtCategoryID.Text, CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.SeqNo].Value));
                else
                    AddDtlRow(this.dgvAlter.Name);
                
            }
        }
        private void tbcMain_DrawItem(object sender, DrawItemEventArgs e)
        {
            BaseFunctions.TabControl_DrawItem(sender, e, true);
        }
        private void FileMenu_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem mnuItem = sender as ToolStripMenuItem;
            if (mnuItem != null)
            {
                switch (mnuItem.Name)
                {
                    case "newToolStripMenuItem":
                        btnNew_Click(sender, e);
                        break;
                    case "cancelToolStripMenuItem":
                        btnCancel_Click(sender, e);
                        break;
                    case "deleteToolStripMenuItem":
                        btnDelete_Click(sender, e);
                        break;
                    case "saveToolStripMenuItem":
                        btnUpdate_Click(sender, e);
                        break;
                    case "exitToolStripMenuItem":
                        btnExit_Click(sender, e);
                        break;
                }
            }
        }

        #endregion Form Events***

        #region ***Form Functions

        public override bool UpdateData()
        {
            bool bUpdateResult = true;
            string strErrMsg = string.Empty ;
            if (base.ConfirmChange())
            {
                bUpdateResult = base.UpdateData();
            }
            if ( m_bDtlChanged )
            {
                dgvDetail.EndEdit();
                if (CheckDtlRow( ref strErrMsg ))
                {
                    Update_Detail(dgvDetail.CurrentRow);  
                }
            }
            if (m_bDtlChanged_Alter)
            {
                dgvAlter.EndEdit();
                if (CheckAlterRow(ref strErrMsg))
                {
                    Update_Alter(dgvAlter.CurrentRow);
                }
            }
            return bUpdateResult;
        }
        public override bool ConfirmChange()
        {
            return (base.ConfirmChange());
        }
        protected override bool FieldCheck(string strFieldValue, string strFieldName, ref string strErrPrompt)
        {

            strErrPrompt = string.Empty;
            DBAccess dBAccess = new DBAccess(false, false);
            DataRow dr = base.GetCurrentMtnDataRow();


            RM.BusinessRule.RMCategoryBRL mBRL = new WP.RM.BusinessRule.RMCategoryBRL();
            bool bCheckResult = mBRL.FieldCheck(dBAccess, dr, strFieldName, ref strErrPrompt, this.formStatus);
            dBAccess.CloseCon();
            return bCheckResult;

        }
        protected override string GetLookUpSQL(string strTableName, string strLookUpControlName)
        {
            string strSql = string.Empty;
            return base.GetLookUpSQL(strTableName, strLookUpControlName);
        }
        public override void SetButtonStatus()
        {
            switch (this.formStatus)
            {
                case FormStatus.AddNew:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = true;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = true;
                    this.btnCancel.Enabled = true;
                    break;
                case FormStatus.Delete:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = (true && this.CurrentMenuInfo.strPermit == "Y");
                    this.btnCancel.Enabled = false;
                    break;
                case FormStatus.Query:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = (true && this.CurrentMenuInfo.strPermit == "Y");
                    this.btnCancel.Enabled = false;
                    break;
                case FormStatus.Update:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = true;
                    this.btnDelete.Enabled = true;
                    this.btnNew.Enabled = true;
                    this.btnCancel.Enabled = true;
                    this.btnAddLine.Enabled = true;
                    break;
                case FormStatus.Loading:
                    break;

            }

            this.btnToExcel.Visible = (this.formStatus == FormStatus.Delete || this.formStatus == FormStatus.Query) && SysVar.AbleToExcel;
            this.btnToExcel.Enabled = this.formStatus == FormStatus.Delete || this.formStatus == FormStatus.Query;

            newToolStripMenuItem.Enabled = btnNew.Enabled;
            cancelToolStripMenuItem.Enabled = btnCancel.Enabled;
            deleteToolStripMenuItem.Enabled = btnDelete.Enabled;
            saveToolStripMenuItem.Enabled = btnUpdate.Enabled;

            this.btnTop.Enabled = true;
            this.btnBottom.Enabled = true;
            this.btnPrev.Enabled = true;
            this.btnNext.Enabled = true;
            this.btnHelp.Enabled = true;
            this.btnExit.Enabled = true;

            this.btnDelete.Visible = false;
            this.btnNew.Visible = false; 
        }
        public override void SetOtherStatus()
        {
            mblnSettingStatus = true;

            switch (this.formStatus)
            {
                case FormStatus.AddNew:
                    this.txtCategoryID.Focus();
                    break;
                case FormStatus.Delete:
                    break;
                case FormStatus.Query:
                    break;
                case FormStatus.Update:
                    break;
                case FormStatus.Loading:
                    break;
            }

            mblnSettingStatus = false;

        }
        public override void RefreshOverView()
        {
            base.RefreshOverView();
            if (this.RecID != -1)
                this.dgList.RecID = this.RecID;
        }

        private void FillLookUpInfo()
        {
            //SetFieldLookUpInfo("CategoryID", "mCategory", "CountyNo", "ubtCountyNo");
        }

        protected override void ShowOtherData()
        {
            base.ShowOtherData();
            if (this.formStatus == FormStatus.Delete) return;
            InitGridDtl();
            InitGridAlterDtl();
            FillDtl(  this.txtCategoryID.Text);
        } 
        #endregion Form Functions***


        #region Grid 
        #region Grid Detail
        private void InitGridDtl()
        {
           
            this.dgvDetail.Columns.Clear();
            this.dgvDetail.Rows.Clear();

            DataGridViewTextBoxColumn colRowSeqNo = new DataGridViewTextBoxColumn();
            colRowSeqNo.HeaderText = "序号";
            colRowSeqNo.Visible = true ;
            colRowSeqNo.Name = "RowSeqNo";
            colRowSeqNo.Width = 50;
            colRowSeqNo.ReadOnly = true;

            DataGridViewTextBoxColumn colSeqNo = new DataGridViewTextBoxColumn();
            colSeqNo.HeaderText = "序号";
            colSeqNo.Visible = false ;
            colSeqNo.Name = "SeqNo";
            colSeqNo.Width = 50;
            colSeqNo.ReadOnly = false ;

            TextLookUpColumn colWKProductID = new TextLookUpColumn();
            colWKProductID.HeaderText = "工序编号";
            colWKProductID.Width = 250;
            colWKProductID.Name = "WKProductID";
            colWKProductID.ReadOnly = false;

            TextLookUpColumn colWKName = new TextLookUpColumn();
            colWKName.HeaderText = "工序名称";
            colWKName.Width = 250;
            colWKName.Name = "WKName";
            colWKName.ReadOnly = true ;

            DataGridViewListColumn colWKRemark = new DataGridViewListColumn();
            colWKRemark.HeaderText = "工序描述";
            colWKRemark.Width = 130;
            colWKRemark.Name = "WKRemark";
            colWKRemark.ReadOnly = true ;         

            DataGridViewCheckBoxColumn colKeyWK = new DataGridViewCheckBoxColumn ();
            colKeyWK .HeaderText ="关键工序";
            colKeyWK.Width =70;
            colKeyWK.Name ="isKeyWK";
            colKeyWK.ReadOnly =false ;
            colKeyWK.Resizable = DataGridViewTriState.False ;


            DataGridViewCheckBoxColumn colInputWK = new DataGridViewCheckBoxColumn();
            colInputWK.HeaderText = "投料工序";
            colInputWK.Width = 70;
            colInputWK.Name = "isInputWK";
            colInputWK.ReadOnly = false;
            colInputWK.Visible = false;
            colInputWK.Resizable = DataGridViewTriState.False;


            DataGridViewListColumn colCategoryName = new DataGridViewListColumn();
            colCategoryName.HeaderText = "类别名称";
            colCategoryName.Width = 130;
            colCategoryName.Name = "CategoryName";
            colCategoryName.ReadOnly = false; 
            colCategoryName.Visible = false ;

            DataGridViewListColumn colCategoryID = new DataGridViewListColumn();
            colCategoryID.HeaderText = "类别ID";
            colCategoryID.Width = 130;
            colCategoryID.Name = "CategoryID";
            colCategoryID.ReadOnly = false;
            colCategoryID.Visible = false;

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            DataGridViewTextBoxColumn colValueChanged = new DataGridViewTextBoxColumn();
            colValueChanged.HeaderText = "ValueChanged";
            colValueChanged.Visible = false;
            colValueChanged.Name = "ValueChanged";

            DataGridViewImageColumn colDel = new DataGridViewImageColumn();
            Bitmap delbitmap = new Bitmap(this.iglSource.Images["del.ico"]);
            colDel.Width = 20;
            colDel.Name = "DelLine";
            colDel.HeaderText = "";
            colDel.Resizable = DataGridViewTriState.False;

            this.dgvDetail.Columns.AddRange(new DataGridViewColumn[] { colRowSeqNo, colSeqNo, colWKProductID, colWKName, colWKRemark, colKeyWK, colInputWK, colCategoryName, colCategoryID, colRecID, colValueChanged, colDel });

            mDtlColumnReadOnlyArray = new bool[dgvDetail.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvDetail.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvDetail.Columns.Count; i++)
            {
                mDtlColumnReadOnlyArray[i] = dgvDetail.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvDetail.Columns.Count) dgvDetail.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvDetail.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvDetail.RowTemplate.Height = 20;
            this.dgvDetail.RowHeadersWidth = 23;
            this.dgvDetail.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvDetail.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvDetail.ColumnHeadersHeight = 30;
            this.dgvDetail.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvDetail.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvDetail.AllowUserToResizeRows = false;

        }
        private void InitGridAlterDtl()
        {
            this.dgvAlter.Columns.Clear();
            this.dgvAlter.Rows.Clear();

            DataGridViewTextBoxColumn colRowSeqNo = new DataGridViewTextBoxColumn();
            colRowSeqNo.HeaderText = "序号";
            colRowSeqNo.Visible = true ;
            colRowSeqNo.Name = "";
            colRowSeqNo.Width = 50;
            colRowSeqNo.ReadOnly = true;

            TextLookUpColumn colWKProductID = new TextLookUpColumn();
            colWKProductID.HeaderText = "工序编号";
            colWKProductID.Width = 250;
            colWKProductID.Name = "WKProductID";
            colWKProductID.ReadOnly = false;

            TextLookUpColumn colWKName = new TextLookUpColumn();
            colWKName.HeaderText = "工序名称";
            colWKName.Width = 250;
            colWKName.Name = "WKName";
            colWKName.ReadOnly = true ;

            DataGridViewListColumn colWKRemark = new DataGridViewListColumn();
            colWKRemark.HeaderText = "工序描述";
            colWKRemark.Width = 130;
            colWKRemark.Name = "WKRemark";
            colWKRemark.ReadOnly = true ;         

            DataGridViewCheckBoxColumn colKeyWK = new DataGridViewCheckBoxColumn ();
            colKeyWK .HeaderText ="关键工序";
            colKeyWK.Width =70;
            colKeyWK.Name ="isKeyWK";
            colKeyWK.ReadOnly =false ;
            colKeyWK.Resizable = DataGridViewTriState.False ;

            DataGridViewCheckBoxColumn colInputWK = new DataGridViewCheckBoxColumn();
            colInputWK.HeaderText = "投料工序";
            colInputWK.Width = 70;
            colInputWK.Name = "isInputWK";
            colInputWK.ReadOnly = false;
            colInputWK.Visible = false;
            colInputWK.Resizable = DataGridViewTriState.False;

            DataGridViewListColumn colCategoryName = new DataGridViewListColumn();
            colCategoryName.HeaderText = "类别名称";
            colCategoryName.Width = 130;
            colCategoryName.Name = "CategoryName";
            colCategoryName.ReadOnly = false; 
            colCategoryName.Visible = false ;

            DataGridViewListColumn colCategoryID = new DataGridViewListColumn();
            colCategoryID.HeaderText = "类别ID";
            colCategoryID.Width = 130;
            colCategoryID.Name = "CategoryID";
            colCategoryID.ReadOnly = false;
            colCategoryID.Visible = false;

            DataGridViewTextBoxColumn colHdrSeqNo = new DataGridViewTextBoxColumn();
            colHdrSeqNo.HeaderText = "HdrSeqNo";
            colHdrSeqNo.Visible = false;
            colHdrSeqNo.Name = "HdrSeqNo";

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            DataGridViewTextBoxColumn colValueChanged = new DataGridViewTextBoxColumn();
            colValueChanged.HeaderText = "ValueChanged";
            colValueChanged.Visible = false;
            colValueChanged.Name = "ValueChanged";

            DataGridViewImageColumn colDel = new DataGridViewImageColumn();
            Bitmap delbitmap = new Bitmap(this.iglSource.Images["del.ico"]);
            colDel.Width = 20;
            colDel.Name = "DelLine";
            colDel.HeaderText = "";
            colDel.Resizable = DataGridViewTriState.False;

            this.dgvAlter.Columns.AddRange(new DataGridViewColumn[] { colRowSeqNo, colWKProductID, colWKName, colWKRemark, colKeyWK, colInputWK, colCategoryName, colCategoryID, colHdrSeqNo, colRecID, colValueChanged, colDel });

            mDtlColumnReadOnlyArray_Alter  = new bool[dgvAlter.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvAlter.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvAlter.Columns.Count; i++)
            {
                mDtlColumnReadOnlyArray_Alter[i] = dgvAlter.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvAlter.Columns.Count) dgvAlter.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvAlter.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvAlter.RowTemplate.Height = 20;
            this.dgvAlter.RowHeadersWidth = 23;
            this.dgvAlter.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvAlter.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvAlter.ColumnHeadersHeight = 30;
            this.dgvAlter.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvAlter.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvAlter.AllowUserToResizeRows = false;
            
        }

        private void FillDtl( string strCategoryID )
        {
            string strSql = "";
            string strUserName = "";
            string strRecid = "";
            bool bAccess = false;
            m_bCanTrigerCellValueChangedEven = false;

            this.dgvDetail.Rows.Clear();
            DBAccess dbCon = new DBAccess(false, false);

            strSql = "select A.*,B.WorkingProcedureName,b.Remark from rmCategoryWK A inner join rmWorkingProcedure B on A.WKProductID=B.WorkingProcedureID where CategoryID='" + strCategoryID + "' order by SeqNo";
            DataTable mUserAlertTable = dbCon.GetDataSet(strSql).Tables[0];

            for (int i = 0; i < mUserAlertTable.Rows.Count; i++)
            {
                DataRow dr = mUserAlertTable.Rows[i];
                if (this.dgvDetail.Rows.Count <= i) this.dgvDetail.Rows.Add();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.RowSeqNo].Value = dgvDetail.Rows.Count.ToString ();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.SeqNo].Value = dr["SeqNo"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKProductID].Value = dr["WKProductID"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKName].Value = dr["WKName"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKRemark].Value = dr["Remark"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.CategoryName].Value = dr["CategoryName"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.CategoryID].Value = txtCategoryID.Text;  //dr["CategoryID"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.IsKeyWK ].Value = dr["IsKeyWK"].ToString();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.IsInputWK].Value = dr["IsInputWK"].ToString();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.RecID].Value = CvtFunc.ToInt(dr["RecID"]);
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.ValueChanged].Value = "0";
            }//
            if (dgvDetail.Rows.Count == 0)
            {
                AddDtlRow( this.dgvDetail.Name );
            }
            dbCon.CloseCon();
            base.SetBlankImage(dgvDetail); 
            m_bCanTrigerCellValueChangedEven = true;
         }
        private void FillDtl_Alter (string strCategoryID, string strHdrSeqNo)
        {
            string strSql = "";
            string strUserName = "";
            string strRecid = "";
            if (String.IsNullOrEmpty(strHdrSeqNo)) return; 
            bool bAccess = false;
            m_bCanTrigerCellValueChangedEven_Alter = false;

            this.dgvAlter.Rows.Clear();
            DBAccess dbCon = new DBAccess(false, false);

            strSql = "select A.*,B.WorkingProcedureName,b.Remark from rmCategoryWKAlter A inner join rmWorkingProcedure B on A.WKProductID=B.WorkingProcedureID where CategoryID='" + strCategoryID + "' and A.HdrSeqNo='" + strHdrSeqNo + "' order by SeqNo";
            DataTable mUserAlertTable = dbCon.GetDataSet(strSql).Tables[0];

            for (int i = 0; i < mUserAlertTable.Rows.Count; i++)
            {
                DataRow dr = mUserAlertTable.Rows[i];
                if (this.dgvAlter.Rows.Count <= i) this.dgvAlter.Rows.Add();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.RowSeqNo].Value = dgvAlter.Rows.Count.ToString();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.WKProductID].Value = dr["WKProductID"].ToString().TrimEnd();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.WKName].Value = dr["WKName"].ToString().TrimEnd();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.WKRemark].Value = dr["Remark"].ToString().TrimEnd();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.CategoryName].Value = dr["CategoryName"].ToString().TrimEnd();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.CategoryID].Value = txtCategoryID.Text; //dr["CategoryID"].ToString().TrimEnd();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.IsKeyWK].Value = dr["IsKeyWK"].ToString();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.IsInputWK].Value = dr["IsInputWK"].ToString();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.HdrSeqNo].Value = dr["HdrSeqNo"].ToString();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.RecID].Value = CvtFunc.ToInt(dr["RecID"]);
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.ValueChanged].Value = "0";
            }//
            if (dgvAlter.Rows.Count == 0)
            {
                AddDtlRow(this.dgvAlter.Name);
            }
            dbCon.CloseCon();
            base.SetBlankImage(dgvAlter);
            m_bCanTrigerCellValueChangedEven_Alter = true;
        }
        
        private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (dgvDetail.Columns[e.ColumnIndex] is TextLookUpColumn)
            {
                if ((dgvDetail.Columns[e.ColumnIndex] as TextLookUpColumn).CanLookUp())
                {
                        Dtl_LookUp(e.ColumnIndex);
                }
            }
            else
            {
                if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton && (this.formStatus == FormStatus.Update) && base.HasDelImage(this.dgvDetail, e))
                {
                    this.dgvDetail.CurrentCell.Selected = true;
                    this.dgvDetail.CurrentCell.Selected = false;
                    if (!this.Delete_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                    {
                        return;
                    }
                    this.dgvDetail.Rows.Remove(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]);
                    if (this.dgvDetail.Rows.Count == 0)
                    {
                        this.AddDtlRow( this.dgvDetail.Name  );
                    }
                }
            }
        }
        private void dgvAlter_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (dgvAlter.Columns[e.ColumnIndex] is TextLookUpColumn)
            {
                if ((dgvAlter.Columns[e.ColumnIndex] as TextLookUpColumn).CanLookUp())
                {
                    DtlAlter_LookUp (e.ColumnIndex);
                }
            }
            else
            {
                if (this.dgvAlter.CurrentCell.ColumnIndex == (int)DtlColAlter.DelButton && (this.formStatus == FormStatus.Update) && base.HasDelImage(this.dgvAlter, e))
                {
                    this.dgvAlter.CurrentCell.Selected = true;
                    this.dgvAlter.CurrentCell.Selected = false;
                    if (!this.Delete_Alter(this.dgvAlter.Rows[this.dgvAlter.CurrentCell.RowIndex]))
                    {
                        return;
                    }
                    this.dgvAlter.Rows.Remove(this.dgvAlter.Rows[this.dgvAlter.CurrentCell.RowIndex]);
                    if (this.dgvAlter.Rows.Count == 0)
                    {
                        this.AddDtlRow(this.dgvAlter.Name);
                    }
                }
            }
        }

        private void AddDtlRow(string strGridName)
        {
            AddDtlRow(strGridName, -1 ); 
        }

        private void AddDtlRow( string strGridName,double iRow )
        {
            if (strGridName == dgvDetail.Name )
            {
                int intRow = CvtFunc.ToInt(iRow); 
                m_bCanTrigerCellValueChangedEven = false;
                if (iRow >= 0)
                {
                    intRow++;
                    this.dgvDetail.Rows.Insert(intRow, 1);
                }
                else
                {
                    this.dgvDetail.Rows.Add();
                    intRow = this.dgvDetail.Rows.Count - 1;
                }
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.RowSeqNo].Value = dgvDetail.Rows.Count.ToString();
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.SeqNo].Value = CvtFunc.ToString(CvtFunc.ToDouble(DataFunctions.GetFieldValue("rmCategoryWK", "max(SeqNo)", "CategoryID='" + txtCategoryID.Text + "'")) + 1);
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.CategoryName].Value = txtCategoryName.Text;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.CategoryID].Value = txtCategoryID.Text;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.IsKeyWK].Value = false;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.IsInputWK].Value = false;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKRemark].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKName].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.RecID].Value = -1;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.ValueChanged].Value = "0";
                dgvDetail.CurrentCell = dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKProductID];
                m_bCanTrigerCellValueChangedEven = true;
            }
            else if (strGridName == dgvAlter.Name)
            {
                m_bCanTrigerCellValueChangedEven_Alter  = false;
                this.dgvAlter.Rows.Add();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.RowSeqNo].Value = dgvAlter.Rows.Count.ToString();
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.HdrSeqNo].Value = CvtFunc.ToString ( this.dgvDetail.CurrentRow.Cells [(int)DtlCol.SeqNo].Value ) ;
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.CategoryName].Value = txtCategoryName.Text;
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.CategoryID].Value = txtCategoryID.Text;
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.IsKeyWK].Value = false;
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.IsInputWK].Value = false;
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.WKRemark].Value = "";
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.WKName].Value = "";
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.RecID].Value = -1;
                dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.ValueChanged].Value = "0";
                dgvAlter.CurrentCell = dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.WKProductID];
                m_bCanTrigerCellValueChangedEven_Alter = true;
            }
        }


        private void Dtl_LookUp(int iColumnIndex)
        {
            string strSQL = "", strCaption = "", strFindValue = "", strFindField = "";
            switch ((DtlCol)iColumnIndex)
            {
                case DtlCol.WKProductID :
                    strSQL = WP.Commons.BaseFunctions.GetLookUpSQL("rmWorkingProcedure");
                    strCaption = "工序选择";
                    if (this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value != null) strFindValue = this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value.ToString();
                    strFindField = "WorkingProcedureID";
                    break;
            }

            if (strSQL == "") return;
            if (mblnLookUpSortByDescription)
            {
                strSQL = DataFunctions.GetSqlByDescriptionSort(strSQL);
                mblnLookUpSortByDescription = false;
            }

            frmLookUp frmlookup = new frmLookUp(strSQL, strCaption, strFindValue, strFindField);
            frmlookup.ShowDialog();

            if (frmlookup.HaveReturnValue)
            {
                switch ((DtlCol)iColumnIndex)
                {
                    case DtlCol.WKProductID :
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value = frmlookup.GetReturnValue("WorkingProcedureID");
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value = frmlookup.GetReturnValue("WorkingProcedureName");
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKRemark].Value = frmlookup.GetReturnValue("Remark");
                        break;
                }
            }
            frmlookup.Close();
        }
        private void DtlAlter_LookUp(int iColumnIndex)
        {
            string strSQL = "", strCaption = "", strFindValue = "", strFindField = "";
            switch ((DtlColAlter )iColumnIndex)
            {
                case DtlColAlter.WKProductID:
                    strSQL = WP.Commons.BaseFunctions.GetLookUpSQL("rmWorkingProcedure");
                    strCaption = "工序选择";
                    if (this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.WKProductID].Value != null) strFindValue = this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.WKProductID].Value.ToString();
                    strFindField = "WorkingProcedureID";
                    break;
            }

            if (strSQL == "") return;
            if (mblnLookUpSortByDescription)
            {
                strSQL = DataFunctions.GetSqlByDescriptionSort(strSQL);
                mblnLookUpSortByDescription = false;
            }

            frmLookUp frmlookup = new frmLookUp(strSQL, strCaption, strFindValue, strFindField);
            frmlookup.ShowDialog();

            if (frmlookup.HaveReturnValue)
            {
                switch ((DtlColAlter)iColumnIndex)
                {
                    case DtlColAlter.WKProductID:
                        this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.WKProductID].Value = frmlookup.GetReturnValue("WorkingProcedureID");
                        this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.WKName].Value = frmlookup.GetReturnValue("WorkingProcedureName");
                        this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.WKRemark].Value = frmlookup.GetReturnValue("Remark");
                        break;
                }
            }
            frmlookup.Close();
        }

        private bool CheckDtlRow(ref string strErrMsg)
        {
            bool bCheckResult = true;
            for (int i = 0; i < dgvDetail.ColumnCount - 1; i++)
            {

                bCheckResult = CheckDtlCell(i, ref strErrMsg);
                if (!bCheckResult) break;

            }
            return bCheckResult;
        }
        private bool CheckAlterRow(ref string strErrMsg)
        {
            bool bCheckResult = true;
            for (int i = 0; i < dgvAlter.ColumnCount - 1; i++)
            {

                bCheckResult = CheckAlterCell(i, ref strErrMsg);
                if (!bCheckResult) break;

            }
            return bCheckResult;
        }

        private bool CheckDtlCell(int iColumn, ref string strErrMsg)
        {
            bool bReturnValue = true;
            strErrMsg = "";
            string strValue = "";
            if (this.dgvDetail.CurrentRow != null && this.dgvDetail.CurrentRow.Cells[iColumn].Value != null)
            {
                strValue = this.dgvDetail.CurrentRow.Cells[iColumn].Value.ToString().TrimEnd();
            }
            switch ((DtlCol)iColumn)
            {
                case DtlCol.WKProductID:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "工序编码不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmWorkingProcedure", strValue, "WorkingProcedureID"))
                    {
                        strErrMsg = "不存在的工序编码";
                        return false;
                    }
                    if (DataFunctions.CheckRecordExist("rmCategoryWK", strValue, "WKProductID", "categoryname='" + txtCategoryID.Text + "' and recid!='" + this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.RecID].Value + "'"))
                    {
                        strErrMsg = "不能重复添加此工序";
                        return false;
                    }
                    return true;
                case DtlCol.WKName :
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "工序名称不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmWorkingProcedure", strValue, "WorkingProcedureName"))
                    {
                        strErrMsg = "不存在的工序名称";
                        return false;
                    }
                    if (DataFunctions.CheckRecordExist("rmCategoryWK", strValue, "WKName", "categoryname='" + txtCategoryName.Text + "' and recid!='" + this.dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value + "'"))
                    {
                        strErrMsg ="不能重复添加此工序";
                        return false ;
                    }
                    return true;
            }
            return bReturnValue;
        }
        private bool CheckAlterCell(int iColumn, ref string strErrMsg)
        {
            bool bReturnValue = true;
            strErrMsg = "";
            string strValue = "";
            if (this.dgvAlter.CurrentRow != null && this.dgvAlter.CurrentRow.Cells[iColumn].Value != null)
            {
                strValue = this.dgvAlter.CurrentRow.Cells[iColumn].Value.ToString().TrimEnd();
            }
            switch ((DtlColAlter)iColumn)
            {
                case DtlColAlter.WKProductID :
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "工序编码不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmWorkingProcedure", strValue, "WorkingProcedureID"))
                    {
                        strErrMsg = "不存在的工序编码";
                        return false;
                    }
                    if (DataFunctions.CheckRecordExist("rmCategoryWKAlter", strValue, "WKProductID", "categoryname='" + txtCategoryID.Text + "' and recid!='" + this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.RecID].Value + "'"))
                    {
                        strErrMsg = "不能重复添加此工序";
                        return false;
                    }
                    return true;
                case DtlColAlter.WKName:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "工序名称不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmWorkingProcedure", strValue, "WorkingProcedureName"))
                    {
                        strErrMsg = "不存在的工序名称";
                        return false;
                    }
                    if (DataFunctions.CheckRecordExist("rmCategoryWKAlter", strValue, "WKName", "categoryname='" + txtCategoryName.Text + "' and recid!='" + this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.RecID].Value + "'"))
                    {
                        strErrMsg ="不能重复添加此工序";
                        return false ;
                    }
                    return true;
            }
            return bReturnValue;
        }

        private void dgvDetail_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgvDetail.CurrentRow == null)
            {
                e.Cancel = false;
                return;
            }
            if (int.Parse(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value.ToString()) > 0 && int.Parse(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value.ToString()) == 0)
            {
                e.Cancel = false;
                return;

            }

            if (mhtiDtl != null && mhtiDtl.ColumnIndex == (int)DtlCol.DelButton)
            {
                mhtiDtl = null;
                return;
            }


            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                e.Cancel = false;
                return;

            }
            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvDetail") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven)
            {
                e.Cancel = false;
                return;
            }

            e.Cancel = !CheckDtlCell(e.ColumnIndex, ref strErrMsg);
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void dgvAlter_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgvAlter.CurrentRow == null)
            {
                e.Cancel = false;
                return;
            }
            if (int.Parse(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.RecID].Value.ToString()) > 0 && int.Parse(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.ValueChanged].Value.ToString()) == 0)
            {
                e.Cancel = false;
                return;

            }

            if (mhtiDtl != null && mhtiDtl.ColumnIndex == (int)DtlColAlter.DelButton)
            {
                mhtiDtl = null;
                return;
            }


            if (this.dgvAlter.CurrentCell.ColumnIndex == (int)DtlColAlter.DelButton)
            {
                e.Cancel = false;
                return;

            }
            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvAlter") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven_Alter)
            {
                e.Cancel = false;
                return;
            }

            e.Cancel = !CheckAlterCell(e.ColumnIndex, ref strErrMsg);
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void dgvDetail_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dgvDetail.CurrentRow != null && int.Parse(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value.ToString()) > 0 && int.Parse(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value.ToString()) == 0)
            {
                e.Cancel = false;
                return;

            }

            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvDetail") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven)
            {
                e.Cancel = false;
                return;
            }
            e.Cancel = !CheckDtlRow(ref strErrMsg);
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }
        private void dgvAlter_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dgvAlter.CurrentRow != null && int.Parse(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.RecID].Value.ToString()) > 0 && int.Parse(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.ValueChanged].Value.ToString()) == 0)
            {
                e.Cancel = false;
                return;

            }

            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvAlter") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven_Alter)
            {
                e.Cancel = false;
                return;
            }
            e.Cancel = !CheckAlterRow(ref strErrMsg);
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }


        private void dgvDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!m_bCanTrigerCellValueChangedEven || e.ColumnIndex == (int)DtlCol.ValueChanged || e.ColumnIndex ==(int)DtlCol.DelButton) return;
            dgvDetail.Rows[e.RowIndex].Cells[(int)DtlCol.ValueChanged].Value = "1";
            m_bDtlChanged = true;
            dgvDetail.EndEdit();
            if (e.ColumnIndex == (int)DtlCol.CategoryID)
            {
                string strCategoryID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value);
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryName].Value = DataFunctions.GetFieldValue("rmCategory", "CategoryName", "CategoryID='" + strCategoryID + "'");
            }
            if (e.ColumnIndex == (int)DtlCol.WKProductID)
            {
                string strWKName = CvtFunc.ToString ( this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value);
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "WorkingProcedureName", "WorkingProcedureID='" + strWKName + "'");
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKRemark].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "Remark", "WorkingProcedureID='" + strWKName + "'");
            }
            dgvDetail.EndEdit();
        }
        private void dgvAlter_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!m_bCanTrigerCellValueChangedEven_Alter  || e.ColumnIndex == (int)DtlColAlter.ValueChanged || e.ColumnIndex ==(int)DtlColAlter.DelButton) return;
            dgvAlter.Rows[e.RowIndex].Cells[(int)DtlColAlter.ValueChanged].Value = "1";
            m_bDtlChanged_Alter = true;
            dgvAlter.EndEdit();
            if (e.ColumnIndex == (int)DtlColAlter.CategoryID)
            {
                string strCategoryID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlColAlter.CategoryID].Value);
                this.dgvDetail.CurrentRow.Cells[(int)DtlColAlter.CategoryName].Value = DataFunctions.GetFieldValue("rmCategory", "CategoryName", "CategoryID='" + strCategoryID + "'");
            }
            if (e.ColumnIndex == (int)DtlColAlter.WKProductID)
            {
                string strWKName = CvtFunc.ToString(this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.WKProductID].Value);
                this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.WKName].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "WorkingProcedureName", "WorkingProcedureID='" + strWKName + "'");
                this.dgvAlter.CurrentRow.Cells[(int)DtlColAlter.WKRemark].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "Remark", "WorkingProcedureID='" + strWKName + "'");
            }
            dgvAlter.EndEdit();
        }

        private void dgvDetail_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                this.dgvDetail.CurrentCell.Selected = false;
            }
        }
        private void dgvAlter_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvAlter.CurrentCell.ColumnIndex == (int)DtlColAlter.DelButton)
            {
                this.dgvAlter.CurrentCell.Selected = false;
            }
        }


        private void dgvDetail_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                this.dgvDetail.CurrentCell.Selected = false;
            }
        }
        private void dgvAlter_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvAlter.CurrentCell.ColumnIndex == (int)DtlColAlter.DelButton)
            {
                this.dgvAlter.CurrentCell.Selected = false;
            }
        }

        private void dgvDetail_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            this.dgvDetail.EndEdit();
        }
        private void dgvAlter_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            this.dgvAlter.EndEdit();
        }

        private void dgvDetail_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            return;
        }
        private void dgvAlter_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            return;
        }

        private void dgvDetail_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            base.SetGridViewDelImage(this.dgvDetail, e, (int)DtlCol.DelButton);     
            string strErrMsg = "";
            if (dgvDetail.CurrentRow != null && CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
            {
                if (CheckDtlRow(ref strErrMsg))
                {
                    if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                    {
                        return;
                    }
                    InitGridAlterDtl();
                    if (this.dgvAlter.CurrentRow != null)
                        FillDtl_Alter(txtCategoryID.Text, CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.SeqNo].Value));
                    else
                        AddDtlRow(this.dgvAlter.Name);
                }
                else
                {
                    return;
                }
            }
        }
        private void dgvAlter_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            base.SetGridViewDelImage(this.dgvAlter, e, (int)DtlColAlter.DelButton);
            string strErrMsg = "";
            if (dgvAlter.CurrentRow != null && CvtFunc.ToInt(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.ValueChanged].Value) > 0)
            {
                if (CheckAlterRow(ref strErrMsg))
                {
                    if (!Update_Alter(this.dgvDetail.Rows[this.dgvAlter.CurrentCell.RowIndex]))
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
        }

        private void dgvDetail_MouseDown(object sender, MouseEventArgs e)
        {

            mhtiDtl = this.dgvDetail.HitTest(e.X, e.Y);

            //System.Windows.Forms.DataGridView.HitTestInfo hti;
            //hti = this.dgvDetail.HitTest(e.X, e.Y);
            //switch (hti.Type)
            //{
            //    case System.Windows.Forms.DataGridViewHitTestType.Cell:
            //        break;
            //}
        }
        private void dgvAlter_MouseDown(object sender, MouseEventArgs e)
        {

            mhtiDtl = this.dgvAlter.HitTest(e.X, e.Y);

            //System.Windows.Forms.DataGridView.HitTestInfo hti;
            //hti = this.dgvDetail.HitTest(e.X, e.Y);
            //switch (hti.Type)
            //{
            //    case System.Windows.Forms.DataGridViewHitTestType.Cell:
            //        break;
            //}
        }

        private bool dgvDetail_KeyDownEvent(System.Windows.Forms.Keys keyData)
        {
            string strErrMsg = "";
            int j;
            switch (keyData)
            {
                case Keys.Down:
                    #region Key Down

                    this.dgvDetail.EndEdit();
                    if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt  (dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                        {

                            if (CheckDtlRow(ref strErrMsg))
                            {

                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                                {
                                    return true;
                                }
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvDetail.CurrentCell.RowIndex < dgvDetail.Rows.Count - 1)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName ];
                        }
                        else
                        {
                            if (this.formStatus == FormStatus.Update) AddDtlRow(this.dgvDetail.Name);
                        }
                    }
                    else
                    {
                        if (CheckDtlRow(ref strErrMsg))
                        {
                            if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                            {
                                return true;
                            }
                            if (this.formStatus == FormStatus.Update) AddDtlRow(this.dgvDetail.Name);
                        }
                        else
                        {
                            MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return true;
                        }
                    }
                    return true;


                    #endregion
                case Keys.Up:
                    #region Key Up
                    if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                        {
                            if (CheckDtlRow(ref strErrMsg))
                            {
                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvDetail.CurrentCell.RowIndex > 0)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex - 1].Cells[(int)DtlCol.WKName ];
                        }
                    }
                    else
                    {
                        if (CheckDtlRow(ref strErrMsg))
                        {
                            if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            if (dgvDetail.CurrentCell.RowIndex > 0)
                            {
                                dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex - 1].Cells[(int)DtlCol.WKName];
                            }
                        }
                        else if (dgvDetail.Rows.Count > 1)
                        {

                            this.dgvDetail.Rows.Remove(this.dgvDetail.CurrentRow);
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKName];
                        }
                    }
                    return true;
                    #endregion
                case Keys.Enter:
                    #region Key Enter
                    this.dgvDetail.EndEdit();
                    j = BaseFunctions.GetGridViewLastEnabledEditColumn(this.dgvDetail);
                    if (dgvDetail.CurrentCell.ColumnIndex == j)
                    {
                        if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                        {
                            if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                            {
                                if (CheckDtlRow(ref strErrMsg))
                                {
                                    if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                                }
                                else
                                {
                                    MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return true;
                                }
                            }
                        }
                        else
                        {
                            if (CheckDtlRow(ref strErrMsg))
                            {
                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }

                        }
                        if (dgvDetail.CurrentCell.RowIndex < dgvDetail.Rows.Count - 1)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName];
                        }
                        else
                        {
                            if (this.formStatus == FormStatus.Update) AddDtlRow(this.dgvDetail.Name);
                        }
                    }
                    else
                    {
                        j = BaseFunctions.GetGridViewNextEnabledEditColumn(dgvDetail, dgvDetail.CurrentCell.ColumnIndex);
                        if (j >= 0) SendKeys.Send("{Tab}");
                        //  if (j >= 0) dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex].Cells[j];
                    }

                    return true;
                    #endregion
            }
            return false;
        }
        private bool dgvAlter_KeyDownEvent(System.Windows.Forms.Keys keyData)
        {
            string strErrMsg = "";
            int j;
            switch (keyData)
            {
                case Keys.Down:
                    #region Key Down

                    this.dgvAlter.EndEdit();
                    if (CvtFunc.ToInt(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.ValueChanged].Value) > 0)
                        {

                            if (CheckAlterRow(ref strErrMsg))
                            {

                                if (!Update_Alter(this.dgvAlter.Rows[this.dgvAlter.CurrentCell.RowIndex]))
                                {
                                    return true;
                                }
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvAlter.CurrentCell.RowIndex < dgvAlter.Rows.Count - 1)
                        {
                            dgvAlter.CurrentCell = this.dgvAlter.Rows[dgvAlter.CurrentCell.RowIndex + 1].Cells[(int)DtlColAlter.WKName];
                        }
                        else
                        {
                            if (this.formStatus == FormStatus.Update) AddDtlRow(this.dgvAlter.Name);
                        }
                    }
                    else
                    {
                        if (CheckAlterRow(ref strErrMsg))
                        {
                            if (!Update_Alter(this.dgvAlter.Rows[this.dgvAlter.CurrentCell.RowIndex]))
                            {
                                return true;
                            }
                            if (this.formStatus == FormStatus.Update) AddDtlRow(this.dgvAlter.Name);
                        }
                        else
                        {
                            MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return true;
                        }
                    }
                    return true;


                    #endregion
                case Keys.Up:
                    #region Key Up
                    if (CvtFunc.ToInt(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.ValueChanged].Value) > 0)
                        {
                            if (CheckAlterRow(ref strErrMsg))
                            {
                                if (!Update_Alter(this.dgvAlter.Rows[this.dgvAlter.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvAlter.CurrentCell.RowIndex > 0)
                        {
                            dgvAlter.CurrentCell = this.dgvAlter.Rows[dgvAlter.CurrentCell.RowIndex - 1].Cells[(int)DtlColAlter.WKName];
                        }
                    }
                    else
                    {
                        if (CheckAlterRow(ref strErrMsg))
                        {
                            if (!Update_Alter(this.dgvAlter.Rows[this.dgvAlter.CurrentCell.RowIndex])) return true;
                            if (dgvAlter.CurrentCell.RowIndex > 0)
                            {
                                dgvAlter.CurrentCell = this.dgvAlter.Rows[dgvAlter.CurrentCell.RowIndex - 1].Cells[(int)DtlColAlter.WKName];
                            }
                        }
                        else if (dgvAlter.Rows.Count > 1)
                        {

                            this.dgvAlter.Rows.Remove(this.dgvAlter.CurrentRow);
                            dgvAlter.CurrentCell = this.dgvAlter.Rows[dgvAlter.Rows.Count - 1].Cells[(int)DtlColAlter.WKName];
                        }
                    }
                    return true;
                    #endregion
                case Keys.Enter:
                    #region Key Enter
                    this.dgvAlter.EndEdit();
                    j = BaseFunctions.GetGridViewLastEnabledEditColumn(this.dgvAlter);
                    if (dgvAlter.CurrentCell.ColumnIndex == j)
                    {
                        if (CvtFunc.ToInt(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.RecID].Value) > 0)
                        {
                            if (CvtFunc.ToInt(dgvAlter.CurrentRow.Cells[(int)DtlColAlter.ValueChanged].Value) > 0)
                            {
                                if (CheckAlterRow(ref strErrMsg))
                                {
                                    if (!Update_Alter(this.dgvAlter.Rows[this.dgvAlter.CurrentCell.RowIndex])) return true;
                                }
                                else
                                {
                                    MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return true;
                                }
                            }
                        }
                        else
                        {
                            if (CheckAlterRow(ref strErrMsg))
                            {
                                if (!Update_Alter(this.dgvAlter.Rows[this.dgvAlter.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }

                        }
                        if (dgvAlter.CurrentCell.RowIndex < dgvAlter.Rows.Count - 1)
                        {
                            dgvAlter.CurrentCell = this.dgvAlter.Rows[dgvAlter.CurrentCell.RowIndex + 1].Cells[(int)DtlColAlter.WKName];
                        }
                        else
                        {
                            if (this.formStatus == FormStatus.Update) AddDtlRow(this.dgvAlter.Name);
                        }
                    }
                    else
                    {
                        j = BaseFunctions.GetGridViewNextEnabledEditColumn(dgvAlter, dgvAlter.CurrentCell.ColumnIndex);
                        if (j >= 0) SendKeys.Send("{Tab}");
                        //  if (j >= 0) dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex].Cells[j];
                    }

                    return true;
                    #endregion
            }
            return false;
        }

        private bool Update_Detail( DataGridViewRow dgvRow )
        {
            if (dgvRow == null) return false ;
            string strSQL = string.Empty;
            string strTableName="rmCategoryWK";
            DBAccess dbcon = new DBAccess(false, false);
            string strFields = "SeqNo,CategoryID,WKProductID,CategoryName,WKName,IsKeyWK,IsInputWK";
            try
            {
                DataRow drDtl = dbcon.GetDataTable("select * from " + strTableName + " ").NewRow();
                if (CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCol.RecID].Value) > 0)   //Update
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, strTableName, drDtl, FormStatus.Update);
                }
                else //Add New
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, strTableName, drDtl, FormStatus.AddNew);
                    dgvRow.Cells[(int)DtlCol.RecID].Value = CvtFunc.ToString(drDtl["RecID"]);
                }
                dbcon.CloseCon();
            }
            catch (Exception ex)
            {
                dbcon.CloseCon();
                return false;
            }
            return true;
        }
        private bool Update_Alter(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return false;
            string strSQL = string.Empty;
            string strTableName = "rmCategoryWKAlter";
            DBAccess dbcon = new DBAccess(false, false);
            string strFields = "HdrSeqNo,CategoryName,CategoryID,WKProductID,WKName,IsKeyWK,IsInputWK,SeqNo";
            try
            {
                DataRow drDtl = dbcon.GetDataTable("select * from " + strTableName + " ").NewRow();
                if (CvtFunc.ToDouble(dgvRow.Cells[(int)DtlColAlter.RecID].Value) > 0)   //Update
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, strTableName, drDtl, FormStatus.Update);
                }
                else //Add New
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, strTableName, drDtl, FormStatus.AddNew);
                    dgvRow.Cells[(int)DtlColAlter.RecID].Value = CvtFunc.ToString(drDtl["RecID"]);
                }
                dbcon.CloseCon();
            }
            catch (Exception ex)
            {
                dbcon.CloseCon();
                return false;
            }
            return true;
        }

        private bool Delete_Detail(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return true;
            string strRecID = CvtFunc.ToString ( dgvRow.Cells[(int)DtlCol.RecID].Value );
            string strSeqNo = CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.SeqNo].Value);
            string strWKName = CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.WKName].Value);
            string strSQL = string.Empty;
            if (CvtFunc.ToDouble(strRecID) > 0)
            {
                if (!DeleteVaild(strWKName))
                {
                    MessageBox.Show("已被使用，不能删除", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
                if (MessageBox.Show("您确定要删除？", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DBAccess dbcon = new DBAccess(false, false);
                    try
                    {
                        dbcon.BeginTran();
                        strSQL ="Delete rmCategoryWKAlter where HdrSeqNo='"+ strSeqNo +"'";
                        dbcon.ExecuteSql(strSQL);

                        strSQL = "Delete rmCategoryWK where RecID='" + strRecID + "'";
                        dbcon.ExecuteSql(strSQL);
                        dbcon.CommitTran(); 
                    }
                    catch (Exception ex)
                    {
                        dbcon.RollbackTran();                        
                    }                   
                    dbcon.CloseCon();
                    return true;
                }
                else 
                {
                    return false ;
                }
            }
            return true;
        }
        private bool Delete_Alter(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return true;
            string strRecID = CvtFunc.ToString(dgvRow.Cells[(int)DtlColAlter.RecID].Value);
            string strSQL = string.Empty;
            if (CvtFunc.ToDouble(strRecID) > 0)
            {
                string strWKName = CvtFunc.ToString(dgvRow.Cells[(int)DtlColAlter.WKName].Value);
                if (!DeleteVaild(strWKName))
                {
                    MessageBox.Show("已被使用，不能删除", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
                if (MessageBox.Show("您确定要删除？", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DBAccess dbcon = new DBAccess(false, false);
                    strSQL = "Detlet rmCategoryWKAlter where RecID='" + strRecID + "'";
                    dbcon.ExecuteSql(strSQL);
                    dbcon.CloseCon();
                }
                else
                    return false;
            }
            return true;
        }

        #endregion

        private void btnAddLine_Click(object sender, EventArgs e)
        {
            string strErrMsg = string.Empty;
            if (this.tbcMain.SelectedTab == tapAlter)
            {
                if (CheckAlterRow(ref strErrMsg))
                {
                    if (Update_Alter (this.dgvAlter.CurrentRow))
                    {
                        AddDtlRow(this.dgvAlter.Name, dgvAlter.CurrentCell.RowIndex);
                        //this.dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName] ; 
                        //base.SetBlankImage(this.dgvDetail);
                    }
                }
                else if (string.IsNullOrEmpty(strErrMsg))
                {
                    MessageBox.Show(strErrMsg, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                if (CheckDtlRow(ref strErrMsg))
                {
                    if (Update_Detail(this.dgvDetail.CurrentRow))
                    {
                        AddDtlRow(this.dgvDetail.Name, dgvDetail.CurrentCell.RowIndex);
                        //this.dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName] ; 
                        //base.SetBlankImage(this.dgvDetail);
                    }
                }
                else if (string.IsNullOrEmpty(strErrMsg))
                {
                    MessageBox.Show(strErrMsg, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }


        #region Grid Alter


        #endregion
        #endregion


        private bool DeleteVaild( string strWKName)
        {
            string strSQL = @"  select distinct WKName from dbo.rmCategorySetBOMDetail where WKName='{0}'
                                union
                                select distinct WKName from dbo.rmCategorySetBOMLabourCost where WKName='{0}'
                                union
                                select distinct WKName from dbo.rmProductBOMDetail where WKName='{0}'
                                union
                                select distinct WKName from dbo.rmProductBOMDetailRev where WKName='{0}'";
            strSQL = string.Format(strSQL, strWKName);
            DBAccess dbcon = new DBAccess(false, false);
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow == null)
                return true;
            else
                return false;
        }
    }
}
